<?php
// +----------------------------------------------------------------------
// | 功能说明"
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// +----------------------------------------------------------------------


namespace app\admin\controller;

use think\Controller;
use think\Db;
use org\Verify;


use app\admin\model\SysUserModel;
use app\admin\model\LogLoginModel;



class Login extends Controller
{


       
    /**
     * 功能：登录首页
     */
    public function index()
    {
        $this->loginoutSession();
        return $this->fetch();
    }


           
    /**
     * 功能：登录验证
     */
    public function login()
    {
        $code = input('param.checkcode');   // $jsonstr=file_get_contents("php://input");


        $username = input('param.username');
        $password = input('param.password');
        $result = $this->validate(compact('username', 'password'), 'SysUserValidate');
        if(true !== $result){
            return json(msg(-1, '', $result));
        }

        $verify = new Verify();
        if (!$verify->check($code)) {
           // return json(msg(-2, '', '验证码错误'));
        }


        $sysuserModel = new SysUserModel();
        $sysuserModel->title='用户';
        $theUser = $sysuserModel->findOneByusername($username);
        if(empty($theUser)){ return json(msg(-3, '', '用户名不存在')); }


        $pwdright=md5($password . config('salt'));
        if($pwdright!= $theUser['password']){
            return json(msg(-3, '', '密码错误'));
        }

        if($theUser['status']<0){
            return json(msg(-4, '', '帐号异常,请联系管理员'));
        }



         // 更新登录状态
         $param = [
            'login_times' => $theUser['login_times'] + 1,
            'login_ip' => request()->ip(),
            'login_time' => time()
        ];
        $res = $sysuserModel->updateLogin($param, $theUser['id']);
        if($res['code']<0){
            return json(msg(-5, '', $res['msg']));
        }

        $paramlogin = [
            'login_times' => $theUser['login_times'] + 1,
            'login_ip' => request()->ip(),
            'login_time' => time(),
            'password' => '',//$password
            'username' => $username,
            
        ];

        $loginlogModel = new LogLoginModel();
        $loginlogModel->insert($paramlogin);

        $this->loginSession($theUser);
        return json(msg(1, url('admin/index/index'), '登录成功'));
    }


    private function  loginSession($theUser)
    {

        session('username', $theUser['username']);
        session('name', $theUser['name']);
        
        session('id', $theUser['id']);
        session('roleid', $theUser['roleid']);
        session('user', $theUser);

    }


    private function  loginoutSession()
    {

        session('username',"");
        session('name',"");
        session('id', "");
        session('roleid', "");
        session('user', "");

    }



    
    // 验证码
    public function checkVerify()
    {
     
        ob_clean();
        $verify = new Verify();
        $verify->imageH = 32;
        $verify->imageW = 100;
        $verify->length = 4;
        $verify->useNoise = false;
        $verify->fontSize = 14;
        return $verify->entry();
    }

    /*************************************************LAYUI基础功能*************************************************/
    /**
     * 清除缓存
     */
    public function clear() {

        if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
            $this->success('清除缓存成功');
        } else {
            $this->error('清除缓存失败');
        }
    }

    /**
     * 退出登录
     * @return \think\response\Json
     */
    public function loginOut(){

        $this->loginoutSession();
        //$this->redirect('home/login/index');
        return json(msg( 1,url('login/index'), '退出登录成功'));
    }
}
